package org.hgvc.webapp.ets.jsf.beans;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.faces.component.UIParameter;
import javax.faces.component.html.HtmlOutputText;
import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;

import org.apache.myfaces.custom.calendar.HtmlInputCalendar;
import org.hgvc.webapp.ets.bo.Agent;
import org.hgvc.webapp.ets.bo.Agreement;
import org.hgvc.webapp.ets.bo.Member;
import org.hgvc.webapp.ets.bo.MemberBooking;
import org.hgvc.webapp.ets.bo.Package;
import org.hgvc.webapp.ets.bo.Remark;
import org.hgvc.webapp.ets.constant.AdminConstant;
import org.hgvc.webapp.ets.constant.BookingConstant;
import org.hgvc.webapp.ets.constant.CommonConstant;
import org.hgvc.webapp.ets.jsf.beans.member.MemberPortalBean;
import org.hgvc.webapp.ets.spring.service.AdminPackageService;
import org.hgvc.webapp.ets.spring.service.BookingService;
import org.hgvc.webapp.ets.spring.service.MasterSetupService;
import org.hgvc.webapp.ets.spring.service.MemberService;
import org.jocsb.base.AbstractBean;
import org.jocsb.base.exception.BaseException;
import org.jocsb.base.util.FacesUtils;


public class MemberBookingInquiryBean extends AbstractBean{
	public static final String BEAN_NAME="memberBookingInquiryBean";
	
	private BookingService bookingService;
	private MemberService memberService;
	private AdminPackageService adminPackageService;
	private MasterSetupService masterSetupService;
	
	private List<MemberBooking> memberBookingList;
	private List<Member> memberList;
	private List<Agreement> agreementList;
	private List<Package> bookingPackageList;
	private List<Remark> remarkList;
	
	private Agent agent;
	private MemberBooking memberBooking;
	private Member member;
	private Agreement agreement;
	private Package bookingPackage;
	private Remark remark;
	
	private List<SelectItem> selectRemark;
	
	private boolean catchError;
	private boolean lookupMemberForm;
	private boolean lookupAgreementForm;
	private boolean lookupPackageForm;
	
	private List<SelectItem> selectSearchList;
	private String searchField;
	private String searchKeyword;
	private boolean confirmBooking;
	private boolean cancelBooking;
	
	private String bookingStatus;
	
	public List<Agreement> getAgreementList() {
		return agreementList;
	}
	public void setAgreementList(List<Agreement> agreementList) {
		this.agreementList = agreementList;
	}
	public List<Package> getBookingPackageList() {
		return bookingPackageList;
	}
	public void setBookingPackageList(List<Package> bookingPackageList) {
		this.bookingPackageList = bookingPackageList;
	}
	public MemberBooking getMemberBooking() {
		return memberBooking;
	}
	public void setMemberBooking(MemberBooking memberBooking) {
		this.memberBooking = memberBooking;
	}
	public Member getMember() {
		return member;
	}
	public void setMember(Member member) {
		this.member = member;
	}
	public Agreement getAgreement() {
		return agreement;
	}
	public void setAgreement(Agreement agreement) {
		this.agreement = agreement;
	}
	public Package getBookingPackage() {
		return bookingPackage;
	}
	public void setBookingPackage(Package bookingPackage) {
		this.bookingPackage = bookingPackage;
	}
	public Remark getRemark() {
		return remark;
	}
	public void setRemark(Remark remark) {
		this.remark = remark;
	}
	public String getSearchField() {
		return searchField;
	}
	public void setSearchField(String searchField) {
		this.searchField = searchField;
	}
	public String getSearchKeyword() {
		return searchKeyword;
	}
	public void setSearchKeyword(String searchKeyword) {
		this.searchKeyword = searchKeyword;
	}
	public List<MemberBooking> getMemberBookingList() {
		return memberBookingList;
	}
	public List<Member> getMemberList() {
		return memberList;
	}
	public List<Remark> getRemarkList() {
		return remarkList;
	}
	public List<SelectItem> getSelectRemark() {
		return selectRemark;
	}
	public boolean isLookupMemberForm() {
		return lookupMemberForm;
	}
	public boolean isLookupAgreementForm() {
		return lookupAgreementForm;
	}
	public boolean isLookupPackageForm() {
		return lookupPackageForm;
	}
	public List<SelectItem> getSelectSearchList() {
		return selectSearchList;
	}
	public boolean isConfirmBooking() {
		return confirmBooking;
	}
	public boolean isCancelBooking() {
		return cancelBooking;
	}
	public void setBookingService(BookingService bookingService) {
		this.bookingService = bookingService;
	}
	public void setMemberService(MemberService memberService) {
		this.memberService = memberService;
	}
	public void setAdminPackageService(AdminPackageService adminPackageService) {
		this.adminPackageService = adminPackageService;
	}
	public void setMasterSetupService(MasterSetupService masterSetupService) {
		this.masterSetupService = masterSetupService;
	}
	
	public String initMemberBookingListAction(){
		String outcome=AdminConstant.NAV_STATUS_SUCCESS;
		try{
			MemberPortalBean bean = (MemberPortalBean)FacesUtils.getManagedBean(MemberPortalBean.BEAN_NAME);
			this.member = bean.getMember();
			
			//System.out.println("get from member portal memberid="+this.member.getMemberId());
			
			this.confirmBooking=false;
			this.cancelBooking=false;
			
			this.memberBookingList=this.bookingService.getMemberBookingList(this.member, BookingConstant.STATUS_PENDING);
			
			this.searchField=BookingConstant.SEARCH_BOOKING_NO;
			this.searchKeyword=null;
			this.selectSearchList=new ArrayList<SelectItem>();
			SelectItem bnSelectItem=new SelectItem(BookingConstant.SEARCH_BOOKING_NO,BookingConstant.SEARCH_BOOKING_NO);
			this.selectSearchList.add(bnSelectItem);
			SelectItem cnSelectItem=new SelectItem(BookingConstant.SEARCH_CONFIRMATION_NO,BookingConstant.SEARCH_CONFIRMATION_NO);
			this.selectSearchList.add(cnSelectItem);
		}catch(BaseException be){
			initErrorBean(be);
		}
		return outcome;
	}
	
	public String navAddMemberBookingAction(){
		String outcome=AdminConstant.NAV_STATUS_SUCCESS;
		if (this.catchError)
			outcome=null;
		return outcome;
	}
	public void navAddMemberBookingActionEvent(ActionEvent event){
		this.catchError=false;
		try{
			this.memberBooking=new MemberBooking();
			this.member=new Member();
			this.agreement=new Agreement();
			this.remark=new Remark();
			this.bookingPackage=new Package();
			this.memberList=this.memberService.getMemberListByAgent(agent);
			this.bookingPackageList=this.adminPackageService.getPackageList();
			this.remarkList=this.masterSetupService.getRemarkList();
			
			this.selectRemark=new ArrayList<SelectItem>();
			for(Remark remark : this.remarkList){
				SelectItem selectItem=new SelectItem(remark.getRemarkId(),remark.getCode()+" - "+remark.getDesc());
				this.selectRemark.add(selectItem);
			}
		}catch(BaseException be){
			this.catchError=true;
			initErrorBean(be);
		}
	}
	public String addMemberBookingAction(){
		String outcome=AdminConstant.NAV_STATUS_SUCCESS;
		if (this.catchError)
			outcome=null;
		return outcome;
	}
	public void addMemberBookingActionEvent(ActionEvent event){
		this.catchError=false;
		try{
			this.memberBooking.setCreatedBy(getThisUser());
			this.memberBooking.setCreatedDate(new Date());
			this.memberBooking.setIsDeleted(CommonConstant.NOT_DELETED);
			if (this.memberBooking.getConfirmationNo()==null) this.memberBooking.setConfirmationNo("");
			if (this.memberBooking.getConfirmationNo().length()>0 && this.memberBooking.getConfirmationDate()!=null)
				this.memberBooking.setStatus(BookingConstant.STATUS_CONFIRM);
			else
				this.memberBooking.setStatus(BookingConstant.STATUS_PENDING);
			
			this.memberBooking.setMember(this.member);
//			this.memberBooking.setAgreement(this.agreement);
			this.memberBooking.setBookingPackage(this.bookingPackage);
			if (this.remark.getRemarkId().length()>0)
				this.memberBooking.setRemark(this.remark);
			else
				this.memberBooking.setRemark(null);
			this.bookingService.saveMemberBooking(this.memberBooking);
			this.memberBookingList=this.bookingService.getMemberBookingList(this.agent, this.bookingStatus);
		}catch(BaseException be){
			this.catchError=true;
			initErrorBean(be);
		}
	}
	
	public String navEditMemberBookingAction(){
		String outcome=AdminConstant.NAV_STATUS_SUCCESS;
		if (this.catchError)
			outcome=null;
		return outcome;
	}
	public void navEditMemberBookingActionEvent(ActionEvent event){
		this.catchError=false;
		UIParameter param=(UIParameter)event.getComponent().findComponent("memberBookingId");
		String memberBookingId=param.getValue().toString();
		try{
			for (MemberBooking memberBookingP : this.memberBookingList){
				if (memberBookingP.getMemberBookingId().equalsIgnoreCase(memberBookingId)){
					this.memberBooking=memberBookingP;
					break;
				}
			}
			this.remarkList=this.masterSetupService.getRemarkList();
			
			this.selectRemark=new ArrayList<SelectItem>();
			for(Remark remark : this.remarkList){
				SelectItem selectItem=new SelectItem(remark.getRemarkId(),remark.getCode()+" - "+remark.getDesc());
				this.selectRemark.add(selectItem);
			}	
			this.member=this.memberBooking.getMember();
//			this.agreement=this.memberBooking.getAgreement();//added by jimm. comment first error
			this.bookingPackage=this.memberBooking.getBookingPackage();
			this.remark=this.memberBooking.getRemark();
			if (this.remark==null) this.remark=new Remark();
			
			if (this.memberBooking.getStatus().equalsIgnoreCase(BookingConstant.STATUS_CONFIRM))
				this.confirmBooking=true;
			if (this.memberBooking.getStatus().equalsIgnoreCase(BookingConstant.STATUS_CANCEL))
				this.cancelBooking=true;
		}catch(BaseException be){
			this.catchError=true;
			initErrorBean(be);
		}
	}
	public String updateMemberBookingAction(){
		String outcome=AdminConstant.NAV_STATUS_SUCCESS;
		if (this.catchError)
			outcome=null;
		return outcome;
	}
	public void updateMemberBookingActionEvent(ActionEvent event){
		this.catchError=false;
		try{
			if (this.memberBooking.getStatus().equalsIgnoreCase(BookingConstant.STATUS_PENDING)){
				if (this.memberBooking.getConfirmationNo().length()>0 && this.memberBooking.getConfirmationDate()!=null)
					this.memberBooking.setStatus(BookingConstant.STATUS_CONFIRM);
			}
			this.memberBooking.setMember(this.member);
//			this.memberBooking.setAgreement(this.agreement);//added by jimm. comment first error
			this.memberBooking.setBookingPackage(this.bookingPackage);
			if (this.remark.getRemarkId().length()>0)
				this.memberBooking.setRemark(this.remark);
			else
				this.memberBooking.setRemark(null);
			this.memberBooking.setModifiedBy(getThisUser());
			this.memberBooking.setModifiedDate(new Date());
			this.bookingService.updateMemberBooking(this.memberBooking);
			this.memberBookingList=this.bookingService.getMemberBookingList(this.agent, this.bookingStatus);
		}catch(BaseException be){
			this.catchError=true;
			initErrorBean(be);
		}
	}
	
	public void cancelMemberBookingActionEvent(ActionEvent event){
		this.catchError=false;
		try{
			this.memberBooking.setStatus(BookingConstant.STATUS_CANCEL);
			this.memberBooking.setModifiedBy(getThisUser());
			this.memberBooking.setModifiedDate(new Date());
			this.bookingService.updateMemberBooking(this.memberBooking);
			this.memberBookingList=this.bookingService.getMemberBookingList(this.agent, this.bookingStatus);
		}catch(BaseException be){
			this.catchError=true;
			initErrorBean(be);
		}
	}
	
	public String searchMemberBookingAction(){
		String outcome=AdminConstant.NAV_STATUS_SUCCESS;
		if (this.catchError)
			outcome=null;
		return outcome;
	}
	public void searchMemberBookingActionEvent(ActionEvent event){
		this.catchError=false;
		try{
			this.memberBookingList=this.bookingService.getMemberBookingList(this.member, this.searchField, this.searchKeyword,this.bookingStatus);
		}catch(BaseException be){
			this.catchError=true;
			initErrorBean(be);
		}
	}
	
	public void lookupFormCloseActionEvent(ActionEvent event){
		this.lookupMemberForm=false;
		this.lookupAgreementForm=false;
		this.lookupPackageForm=false;
	}
	
	public void lookupMemberFormActionEvent(ActionEvent event){
		this.catchError=false;
		this.lookupMemberForm=true;
		this.lookupAgreementForm=false;
		this.lookupPackageForm=false;
		try{
			this.memberList=this.memberService.getMemberActiveList();
		}catch(BaseException be){
			this.catchError=true;
			initErrorBean(be);
		}
	}
	public void lookupMemberSelectedActionEvent(ActionEvent event){
		this.lookupMemberForm=false;
		this.catchError=false;
		UIParameter param=(UIParameter)event.getComponent().findComponent("memberId");
		String memberId=param.getValue().toString();
		for (Member memberP : this.memberList){
			if (memberP.getMemberId().equalsIgnoreCase(memberId)){
				this.member=memberP;
				break;
			}
		}
	}
	
	public void lookupAgreementFormActionEvent(ActionEvent event){
		this.catchError=false;
		this.lookupMemberForm=false;
		this.lookupAgreementForm=true;
		this.lookupPackageForm=false;
		try{
			this.agreementList=this.memberService.getAgreementListByMemberId(this.member.getMemberId());
		}catch(BaseException be){
			this.catchError=true;
			initErrorBean(be);
		}
	}
	public void lookupAgreementSelectedActionEvent(ActionEvent event){
		this.lookupAgreementForm=false;
		UIParameter param=(UIParameter)event.getComponent().findComponent("agreementId");
		String agreementId = param.getValue().toString();
		for (Agreement agreementP : this.agreementList){
			if (agreementP.getAgreementId().equalsIgnoreCase(agreementId)){
				this.agreement=agreementP;
				break;
			}
		}
	}
	
	public void lookupPackageFormActionEvent(ActionEvent event){
		this.catchError=false;
		this.lookupMemberForm=false;
		this.lookupAgreementForm=false;
		this.lookupPackageForm=true;
		try{
			this.bookingPackageList=this.adminPackageService.getPackageList();
		}catch(BaseException be){
			this.catchError=true;
			initErrorBean(be);
		}
	}
	public void lookupPackageSelectedActionEvent(ActionEvent event){
		this.lookupPackageForm=false;
		UIParameter param=(UIParameter)event.getComponent().findComponent("packageId");
		String packageId = param.getValue().toString();
		for(Package packageP : this.bookingPackageList){
			if (packageP.getPackageId().equalsIgnoreCase(packageId)){
				this.bookingPackage=packageP;
				break;
			}
		}
	}
	
	public void pendingStatusSelectedActionEvent(ActionEvent event){
		this.bookingStatus=BookingConstant.STATUS_PENDING;
		System.out.println("this.bookingStatus="+this.bookingStatus);
		try{
			this.memberBookingList=this.bookingService.getMemberBookingList(this.member, this.bookingStatus);
		}catch(BaseException be){
			initErrorBean(be);
		}
	}
	public void confirmedStatusSelectedActionEvent(ActionEvent event){
		this.bookingStatus=BookingConstant.STATUS_CONFIRM;
		System.out.println("this.bookingStatus="+this.bookingStatus);
		try{
			this.memberBookingList=this.bookingService.getMemberBookingList(this.member, this.bookingStatus);
		}catch(BaseException be){
			initErrorBean(be);
		}
	}
	public void cancelledStatusSelectedActionEvent(ActionEvent event){
		this.bookingStatus=BookingConstant.STATUS_CANCEL;
		System.out.println("this.bookingStatus="+this.bookingStatus);
		try{
			this.memberBookingList=this.bookingService.getMemberBookingList(this.member, this.bookingStatus);
		}catch(BaseException be){
			initErrorBean(be);
		}
	}
	public String getBookingStatus() {
		return bookingStatus;
	}
	public void setBookingStatus(String bookingStatus) {
		this.bookingStatus = bookingStatus;
	}
	
	public void checkInCalendarActionEvent(ActionEvent event){
		HtmlInputCalendar htmlInputCal=(HtmlInputCalendar) event.getComponent().findComponent("checkIn");
		Date checkInDate =(Date) htmlInputCal.getValue();

		Calendar cal=Calendar.getInstance();
		cal.setTime(checkInDate);
		cal.add(Calendar.DAY_OF_MONTH, this.bookingPackage.getNoOfNights()-1);
		this.memberBooking.setCheckOutDate(cal.getTime());
		
		HtmlOutputText outtext = (HtmlOutputText)event.getComponent().findComponent("checkOut");
		outtext.setValue(this.memberBooking.getCheckOutDate());
	}
}
